package club.itstudio.itouc.controller.jw.stu

import club.itstudio.itouc.core.util.Result
import club.itstudio.itouc.core.util.ResultUtil
import club.itstudio.itouc.core.util.mapper
import club.itstudio.itouc.jw.ParamsBuilder
import club.itstudio.itouc.jw.with
import club.itstudio.itouc.service.JwOps
import com.fasterxml.jackson.module.kotlin.readValue
import org.springframework.web.bind.annotation.CookieValue
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController

@RestController
class RetakeCourse (val jwOps: JwOps,
                    val jw: ParamsBuilder){
    /**
     * 判断是否可以重修某一门课
     */
    @GetMapping("/jw/stu/course/retake/{classCode}")
    fun canRetakeCourse(@CookieValue token: String,
                        @PathVariable classCode: String): Result<Any> {
        val session = jwOps.getValidJwSession(token)
        val data = jw.canRetake(session.userCode, classCode) with session
        val result:Map<String, String> = mapper.readValue(data)
        val canRetake = (result["status"] == "200" && result["result"] == "1")
        return ResultUtil.success(canRetake)
    }
}